#include ".\netclient.h"

CNetClient::CNetClient(void)
{
	InitializeCriticalSection(&m_cs);
	ZeroMemory( m_RecvBuf, MAX_BUFFER_SIZE );
	ZeroMemory( m_SendBuf, MAX_BUFFER_SIZE );
	m_bReqPacket = FALSE;
	m_dwKey = 0;
}

CNetClient::~CNetClient(void)
{
	DeleteCriticalSection(&m_cs);
}

struct PacketData
{
	SHORT nType;
	int	  nData;
};


BOOL CNetClient::Init()
{
	if( WSAStartup( MAKEWORD(2, 2), &m_wsaData) != 0 )
		return FALSE;

	m_hSocket = socket( PF_INET, SOCK_STREAM, 0 );
	if( m_hSocket == INVALID_SOCKET )
		return FALSE;

	if( !m_Info.LoadData() )
		return FALSE;

	return TRUE;
}

BOOL CNetClient::Connect( )
{
	ZeroMemory( &m_servAddr, sizeof(m_servAddr) );
	m_servAddr.sin_family = AF_INET;
	m_servAddr.sin_addr.s_addr = inet_addr( m_Info.m_szRTTA_IP );
	m_servAddr.sin_port = htons( m_Info.m_nRTTA_Port );

	if( connect( m_hSocket, (SOCKADDR*)&m_servAddr, sizeof(m_servAddr)) == SOCKET_ERROR )
	{
		DWORD dwError = GetLastError();
		return FALSE;
	}

	return TRUE;
}


BOOL CNetClient::DisConnect()
{
//	m_bRunThread = FALSE;
//	WaitForSingleObject( m_hRecvThread, INFINITE );
//	WaitForSingleObject( m_hSendThread, INFINITE );
	return TRUE;
}

BOOL CNetClient::Run()
{
	m_bRunThread = TRUE;

	unsigned int nRecvID = 1;
	m_hRecvThread = (HANDLE)_beginthreadex( NULL, 0, RecvThread, (void*)this, 0, &nRecvID );
	if(m_hRecvThread == INVALID_HANDLE_VALUE )
		return FALSE;

	return TRUE;
}

BOOL CNetClient::AddRecvBuf( char* pBuf, int nSize )
{
	EnterCriticalSection( &m_cs );
	string strMsg = pBuf;
	m_vecRecv.push_back(strMsg);
	LeaveCriticalSection( &m_cs );
	return TRUE;
}

BOOL CNetClient::GetPacket( char* pData, int* pSize )
{	
	EnterCriticalSection( &m_cs );
	int nSize = (int)m_vecRecv.size();
	if(nSize <= 0)
	{
		LeaveCriticalSection(&m_cs);
		return FALSE;
	}

	string strPacket = m_vecRecv.front();
	strcpy( pData, strPacket.data() );
	*pSize = sizeof(pData);
	LeaveCriticalSection(&m_cs);
	return TRUE;
}

BOOL CNetClient::AddSendBuf( char* pBuf, int nSize )
{
	return TRUE;
}

void CNetClient::Close()
{
	closesocket( m_hSocket );
	WSACleanup();
}


unsigned int __stdcall RecvThread(void* pParam)
{
	CNetClient* pModule = (CNetClient*)pParam;

	while( pModule->m_bRunThread )
	{
		Sleep(100);
		char msg[1024];
		ZeroMemory( msg, 1024 );
		int nRecvSize = recv( pModule->m_hSocket, msg, 1024, 0 );
		if( nRecvSize == -1 )
			continue;
		
		MSG_HEADER4* pHeader = (MSG_HEADER4*)msg;
		if( pHeader->hdrID.byMessageID == MO_RTTG_PATCHRESULT_REQ )
		{
			MSG_MO_RTTG_PATCHRESULT_REQ* pPacket = (MSG_MO_RTTG_PATCHRESULT_REQ*)msg;
			pModule->m_dwKey = pPacket->dwKey;
			pModule->m_bReqPacket = TRUE;
			return 0;
		}
	}

	return 0;
}

/*
unsigned int __stdcall SendThread(void* pParam)
{
	CNetClient* pModule = (CNetClient*)pParam;

	while( pModule->m_bRunThread )
	{
		Sleep(1000);
		char msg[1024];
		int nSize = 0;
//		GetSendPacket( msg, &nSize );
		int nSendSize = send( pModule->m_hSocket, msg, 1024, 0 );
		if( nSendSize == -1 )
			continue;
	}

	return 0;
}
*/

BOOL CNetClient::SendData( char* pData, int nSize )
{
	int nSendSize = send( m_hSocket, pData, nSize, 0 );
	if( nSendSize == -1 )
		return FALSE;

	return TRUE;
}